<?php
/**
 * test1.php for mySSO.
 * @author SamWu
 * @date 2018/2/6 16:49
 * @copyright boyaa.com
 */

$appid = 'test';
$appkey = 'agawefawehgawehawef';

session_start();

/*if (!empty($_SESSION['userInfo']))
{
	echo $_SESSION['userInfo'];exit;
}*/

if (isset($_GET['token'])) {
	if (!verifySign($_GET, $appkey)){
		exit('sign error!');
	}
	$data['token'] = $_GET['token'];
	$data['appid'] = $appid;
	$data['t'] = time();
	$data['sign'] = sign($data, $appkey);
	$url = 'http://sso.istimer.com/api/user/token?'.http_build_query($data);
	$res = file_get_contents($url);
	$_SESSION['userInfo'] = $res;
	echo ($res);exit;
}

$sso = 'http://sso.istimer.com/login';

$url = $sso.'?appid='.$appid;

header('location:'.$url);


function sign($data, $appkey)
{
	ksort($data);
	return md5(http_build_query($data).$appkey);
}

function verifySign($data, $appkey)
{
	$sign = isset($data['sign']) ? $data['sign'] : '';
	if (!$sign) {
		return false;
	}
	unset($data['sign']);
	ksort($data);
	return md5(http_build_query($data).$appkey) == $sign;
}
